package com.rupeng.web.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rupeng.pojo.Card;
import com.rupeng.pojo.CardSubject;
import com.rupeng.pojo.Subject;
import com.rupeng.service.CardService;
import com.rupeng.service.CardSubjectService;
import com.rupeng.service.SubjectService;
import com.rupeng.util.AjaxResult;
import com.rupeng.util.CommonUtils;

@Controller
@RequestMapping("/card")
public class CardController {
	@Autowired
	private CardService cardService;
	@Autowired
	private SubjectService subjectService;
	@Autowired
	private CardSubjectService cardSubjectService;
	@RequestMapping("/list.do")
	public ModelAndView list() {
		List<Card> cards = cardService.selectList();
		return new ModelAndView("card/list", "cards", cards);
	}
	@RequestMapping("/delete.do")
	public @ResponseBody AjaxResult delete(Long id){
		cardService.delete(id);
		return AjaxResult.successInstance("删除成功");
	}
	@RequestMapping(value="/add.do",method=RequestMethod.GET)
	public ModelAndView addPage() {
		List<Subject> subjects = subjectService.selectList();
		return new ModelAndView("card/add", "subjects", subjects);
	}
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult addSubmit(String name, Long[] subjectIds, String description, String courseware) {
		if (CommonUtils.isEmpty(name) || CommonUtils.isEmpty(description) || CommonUtils.isEmpty(courseware)) {
			return AjaxResult.errorInstance("表单数据不完整");
		}
		Card card = new Card();
		card.setName(name);
		if (cardService.isExisted(card)) {
			return AjaxResult.errorInstance("该名称已存在");
		}
		card = new Card();
		card.setName(name);
		card.setCreateTime(new Date());
		card.setDescription(description);
		card.setCourseware(courseware);
		cardService.insert(card, subjectIds);
		return AjaxResult.successInstance("添加成功");
	}
	@RequestMapping(value="/update.do",method=RequestMethod.GET)
	public ModelAndView updatePage(Long id) {
		ModelAndView modelAndView = new ModelAndView();
		Card card = cardService.selectOne(id);
		List<Subject> subjects = subjectService.selectList();
		CardSubject cardSubject = new CardSubject();
		cardSubject.setCardId(id);
		List<CardSubject> cardSubjects = cardSubjectService.selectList(cardSubject);
		modelAndView.addObject("card", card);
		modelAndView.addObject("subjects", subjects);
		modelAndView.addObject("cardSubjects", cardSubjects);
		return modelAndView;
	}
	@RequestMapping(value="/update.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult updateSubmit(Long id, String name, Long[] subjectIds, String description, String courseware) {
		if (CommonUtils.isEmpty(name) || CommonUtils.isEmpty(description) || CommonUtils.isEmpty(courseware)) {
			return AjaxResult.errorInstance("表单数据不完整");
		}
		Card card = new Card();
		card.setName(name);
		card = cardService.selectOne(card);
		if (card!=null && card.getId()!=id) {
			return AjaxResult.errorInstance("该名称已存在");
		}
		card = cardService.selectOne(id);
		card.setName(name);
		card.setDescription(description);
		card.setCourseware(courseware);
		cardService.update(card, subjectIds);
		return AjaxResult.successInstance("修改成功");
	}
}
